import { reactive, markRaw } from 'vue';
import { CTableProps } from '@/components/CTable';
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable';
import { ColumnSettingTypeEnum } from '@/components/CPageTable';
import { getAccountTree } from '@/apis/taa/asset/account';

export const useTableList = (handleExpandAll: () => void) => {
  const searchForm = reactive({});
  const searchOptions: SearchOptionsType[] = markRaw([]);

  // 表格配置
  const tableOptions: CTableProps = {
    loadData,
    pagination: false,
    extraTableOption: {
      treeConfig: {
        rowField: 'assetNo',
        children: 'subAccountList',
        expandAll: true
      }
    }
  };

  async function loadData(): Promise<any> {
    return getAccountTree().then((res: any) => {
      if (res && res.length) {
        handleExpandAll();
        return {
          records: res,
          total: res.length
        };
      } else {
        return {
          records: [],
          total: 0
        };
      }
    });
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '排放单位',
      field: 'assetDepartment',
      minWidth: 200,
      treeNode: true,
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '重庆碳配额（CQEA）',
      field: 'carbonQuota',
      slot: 'carbonQuota',
      minWidth: 100,
      align: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '核证减排量（CCER）',
      field: 'carbonReduction',
      // slot: 'carbonReduction',
      minWidth: 100,
      align: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '地方碳普惠（CQCER）',
      field: 'carbonInclusive',
      // slot: 'carbonInclusive',
      minWidth: 100,
      align: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 250,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ];

  return {
    searchForm,
    searchOptions,
    tableOptions,
    tableFieldsSetting
  };
};
